package com.ant.storage.service.impl;

import com.ant.storage.common.ResultVo;
import com.ant.storage.dto.InventoryDto;
import com.ant.storage.dto.WarehouseDto;
import com.ant.storage.mapper.WarehouseMapper;
import com.ant.storage.pojo.InventoryLog;
import com.ant.storage.pojo.Warehouse;
import com.ant.storage.service.ICommodityService;
import com.ant.storage.service.IInventoryLogService;
import com.ant.storage.service.IInventoryService;
import com.ant.storage.service.IWarehouseService;
import com.ant.storage.vo.WarehouseAndCommodityVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author melon
 * @since 2022-03-16
 */
@Service
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse> implements IWarehouseService {

    @Autowired
    private IInventoryLogService inventoryLogService;

    @Autowired
    private IInventoryService inventoryService;

    @Autowired
    private ICommodityService commodityService;

    @Override
    public PageInfo<Warehouse> getWarehousePage(WarehouseDto warehouseDto) {
        PageHelper.startPage(warehouseDto.getPageIndex(), warehouseDto.getPageSize());
        // 先写条件
        QueryWrapper<Warehouse> wrapper = new QueryWrapper<>();
        if (!CollectionUtils.isEmpty(warehouseDto.getCityId())) {
            wrapper.lambda().in(Warehouse::getCityId, warehouseDto.getCityId());
        }
        if (StringUtils.isNotEmpty(warehouseDto.getWarehouseName())) {
            wrapper.lambda().eq(Warehouse::getWarehouseName, warehouseDto.getWarehouseName());
        }
        wrapper.orderByAsc("id");
        PageInfo<Warehouse> pageInfo = new PageInfo<>(this.baseMapper.selectList(wrapper));
        return pageInfo;
    }

    @Override
    public Warehouse findWarehouseAccessRecord(WarehouseDto dto) {
        Warehouse warehouses = this.baseMapper.selectOne(new QueryWrapper<Warehouse>().eq("id", dto.getWarehouseId()));
        List<WarehouseAndCommodityVo> commodityVoList = this.baseMapper.warehouseRecordList(dto.getWarehouseId(),
                dto.getPageIndex(), dto.getPageSize());
        warehouses.setInventoryLogs(commodityVoList);
        warehouses.setTotal(this.baseMapper.warehouseRecordTotal(dto.getWarehouseId()));
        return warehouses;
    }

    @Override
    public List<Warehouse> getWarehouseList(String cityId) {
        if(cityId.equals("0")){
            return this.baseMapper.selectList(null);
        }else{
            return this.baseMapper.selectList(new QueryWrapper<Warehouse>().in("city_id", Arrays.asList(cityId.split(","))));
        }
    }

    @Override
    public ResultVo getWarehouseDeptList(WarehouseDto dto) {
        System.out.println(dto);
        Warehouse warehouses = this.baseMapper.selectOne(new QueryWrapper<Warehouse>().eq("id", dto.getWarehouseId()));
        List<WarehouseAndCommodityVo> commodityVoList =
                this.baseMapper.warehouseAndCommodityList(dto.getWarehouseId(), dto.getPageIndex(), dto.getPageSize(),dto.getWarehouseName(),dto.getCommodityBarCode(),dto.getCommoditytype());
        warehouses.setInventoryList(commodityVoList);
        warehouses.setTotal(this.baseMapper.warehouseAndCommodityTotal(dto.getWarehouseId()));
        List<InventoryLog> logList = inventoryLogService.list(new QueryWrapper<InventoryLog>().eq("warehouse_id", dto.getWarehouseId()));
        warehouses.setDetailsList(logList);
        int wct=logList.stream().filter(s -> s.getStockState() == 1).mapToInt(InventoryLog::getStockState).sum();
        warehouses.setWarehousingCount(wct);
        int out=logList.stream().filter(f -> f.getStockState() == 2).mapToInt(InventoryLog::getStockState).sum();
        warehouses.setOutboundCount(out);
        return ResultVo.success("查询成功", warehouses);
    }

    @Override
    public Map<String,Object> getInventoryByWarehouseId(InventoryDto dto) {
        Map<String,Object> map=new HashMap<>();
        map.put("list",this.baseMapper.getInventoryByWarehouseId(dto));
        map.put("total",this.baseMapper.getInventoryByWarehouseIdCount(dto));
        return map;
    }

    @Override
    public  Map<String,Object> getWarehouseListes(WarehouseDto dto) {
        List<WarehouseAndCommodityVo> list =
                this.baseMapper.warehouseAndCommodityList(dto.getWarehouseId(), dto.getPageIndex(), dto.getPageSize()
                        , dto.getCommodityName(), dto.getCommodityBarCode(),dto.getCommoditytype());
        Map<String,Object> map=new HashMap<>();
        map.put("list",list);
        map.put("total",this.baseMapper.warehouseAndCommodityCount(dto.getWarehouseId(),dto.getCommodityName(),dto.getCommodityBarCode(),dto.getCommoditytype()));
        return map;
    }
}
